﻿using System;

namespace Sgr.Application.Commands
{
    /// <summary>
    /// 命令执行结果
    /// </summary>
    public class CommandResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess => Type == ResultType.Success;

        /// <summary>
        /// 结果类型
        /// </summary>
        public ResultType Type { get; }

        /// <summary>
        /// 消息
        /// </summary>
        public string? Message { get; }

        protected CommandResult(ResultType type, string message)
        {
            Type = type;
            Message = message;
        }

        #region 静态工厂方法

        /// <summary>
        /// 创建成功结果
        /// </summary>
        public static CommandResult Success(string message = "OK")
            => new(ResultType.Success, message);

        /// <summary>
        /// 创建验证失败结果
        /// </summary>
        public static CommandResult Invalid(string message)
            => new(ResultType.Invalid, message);

        /// <summary>
        /// 创建未找到结果
        /// </summary>
        public static CommandResult NotFound(string message)
            => new(ResultType.NotFound, message);

        /// <summary>
        /// 创建未授权结果
        /// </summary>
        public static CommandResult Unauthorized(string message)
            => new(ResultType.Unauthorized, message);

        /// <summary>
        /// 创建冲突结果
        /// </summary>
        public static CommandResult Conflict(string message)
            => new(ResultType.Conflict, message);

        /// <summary>
        /// 创建错误结果
        /// </summary>
        public static CommandResult Error(string message, Exception? ex = null)
            => new(ResultType.Error, ex == null ? message : $"{message}: {ex.Message}");

        #endregion 静态工厂方法

        #region 泛型静态工厂方法

        /// <summary>
        /// 创建成功结果
        /// </summary>
        public static CommandResult<T> Success<T>(T data, string message = "OK")
            => new(ResultType.Success, message, data);

        /// <summary>
        /// 创建验证失败结果
        /// </summary>
        public static CommandResult<T> Invalid<T>(string message)
            => new(ResultType.Invalid, message, default);

        /// <summary>
        /// 创建未找到结果
        /// </summary>
        public static CommandResult<T> NotFound<T>(string message)
            => new(ResultType.NotFound, message, default);

        /// <summary>
        /// 创建未授权结果
        /// </summary>
        public static CommandResult<T> Unauthorized<T>(string message)
            => new(ResultType.Unauthorized, message, default);

        /// <summary>
        /// 创建冲突结果
        /// </summary>
        public static CommandResult<T> Conflict<T>(string message)
            => new(ResultType.Conflict, message, default);

        /// <summary>
        /// 创建错误结果
        /// </summary>
        public static CommandResult<T> Error<T>(string message, Exception? ex = null)
            => new(ResultType.Error, ex == null ? message : $"{message}: {ex.Message}", default);

        #endregion 泛型静态工厂方法
    }

    /// <summary>
    /// 带返回数据的命令执行结果
    /// </summary>
    public sealed class CommandResult<T> : CommandResult
    {
        /// <summary>
        /// 返回数据
        /// </summary>
        public T? Data { get; }

        internal CommandResult(ResultType type, string message, T? data)
            : base(type, message)
        {
            Data = data;
        }
    }
}